<?php
namespace app\admin\controller;

use app\admin\model\Role;
use app\http\GetUid;
use app\admin\model\Admin;
use app\admin\service\PermissionsAppConfig;

/**
 * 权限相关接口
 * @ date: 2019年11月5日 下午2:15:58
 * @ author: 月光下的魔术师
 */
class Role1 extends Common
{
    use GetUid;

    protected $role;

    protected $a;

    protected $path;

    protected $uId;

    protected $my;

    protected $cId;

    public function __construct()
    {
        parent::__construct();
        $this->role = new Role();
        $this->a = new Admin();
        $this->path = dirname($_SERVER['SCRIPT_FILENAME']) . '/permissions.json';
        $this->uId = GetUid::getUId();
        $this->getAdmin();
    }

    // 角色列表
    public function test()
    {
        // return $this->returnJson(200, 'ok', PermissionsAppConfig::_config);
        print_r(count(PermissionsAppConfig::_module));
        exit();
    }

    // 角色列表
    public function rList()
    {
        $r = $this->role->where('u_id', $this->cId)
            ->field('id,name,rules,rules1,info')
            ->select();
        return $this->returnJson(200, 'ok', $r);
    }

    public function details($id)
    {
        $r = $this->role->field('id,name,rules,rules1,info')->find($id);
        $m = file_get_contents($this->path);
        $rules = explode(',', $r['rules']);
        $m = json_decode($m, true);
        foreach ($m as $v) {
            if (in_array($v['id'], $rules)) {
                $rules = array_diff($rules, [
                    $v['id']
                ]);
            }
        }
        $r['rules'] = explode(',', implode(',', $rules));
        $r['rules1'] = empty($r['rules1']) ? [] : explode(',',  $r['rules1']);
        return $this->returnJson(200, 'ok', $r);
    }

    public function save()
    {
        $r = $this->role->saveData(input('post.'), $this->cId);
        return $this->returnJson($r[0], $r[1]);
    }

    public function delete($id)
    {
        $r = $this->role->where('u_id', $this->cId)
            ->where('id', $id)
            ->delete();
        return $this->returnJson(200, 'ok');
    }

    public function permissions()
    {
        $r = file_get_contents($this->path);
        return $this->returnJson(200, 'ok', json_decode($r, true));
    }

    public function appPermissions()
    {
        return $this->returnJson(200, 'ok', PermissionsAppConfig::_module);
    }

    public function appRemovePermissions()
    {
        $menu = [];
        if (! empty($this->my['role_id'])) {
            $role = $this->role->field('id,name,rules1,info')->find($this->my['role_id']);
            $rules = explode(',', $role['rules1']);
            foreach (PermissionsAppConfig::_module as $k => $v) {
                if (! in_array($v['id'], $rules)) {
                    $menu[] = $v['name'];
                }
            }
        }
        return $this->returnJson(200, 'ok', $menu);
    }

    public function getAdmin()
    {
        $this->my = $this->a->get($this->uId);
        if ($this->my['level'] == 1) {
            $this->cId = 0;
        } elseif ($this->my['level'] == 2) {
            $this->cId = $this->uId;
        } else {
            $this->cId = $this->my['store'];
        }
    }
}
?>